/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkl51.validasi;

import pkl51.component.TextField;
import pkl51.controller.CardKues;
import pkl51.view.kuesioner.PanelBlok5;

/**
 *
 * @author ERI
 */
public class ValidasiBlok5 extends AbstractValidasi {

    private PanelBlok5 panelBlok5;
    private int B5R1a;
    private int B5R1b;
    private int B5R1c;

    public void setPanelBlok5(PanelBlok5 panelBlok5) {
        this.panelBlok5 = panelBlok5;
    }

    public ValidasiBlok5() {
        this.B5R1a = -1;
        this.B5R1b = -1;
        this.B5R1c = -1;
    }

    public void registerValidator() {
        panelBlok5.getTextFieldB5R1a().setValidator(new ValidasiB5R1a());
        panelBlok5.getTextFieldB5R1b().setValidator(new ValidasiB5R1b());
        panelBlok5.getTextFieldB5R1c().setValidator(new ValidasiB5R1c());
        for (int i = 0; i < 15; i++) {
            panelBlok5.getGroup()[i].setValidator(new ValidasiTextField());             
        }
    }

    /*public void unRegisterValidator() {
        panelBlok5.getTextFieldB5R1a().setValidator(new NullValidasi());
        panelBlok5.getTextFieldB5R1b().setValidator(new NullValidasi());
        panelBlok5.getTextFieldB5R1c().setValidator(new NullValidasi());
    }

    private class NullValidasi implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            return true;
        }
    }*/

    public void setB5R1a(int B5R1a) {
        this.B5R1a = B5R1a;
    }

    public void setB5R1b(int B5R1b) {
        this.B5R1b = B5R1b;
    }

    public void setB5R1c(int B5R1c) {
        this.B5R1c = B5R1c;
    }

    private class ValidasiTextField implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if(Integer.parseInt(field.getText())<1 || Integer.parseInt(field.getText())>4 ){
                errorMessage("Nilai hanya antara 1 -4!");
                return false;
            }
            else{
                if(B5R1c==0){
                    if(B5R1a==0 && field.toString().equals(panelBlok5.getTextFieldB5R2iih().toString())) CardKues.getCard().next();
                    else if(B5R1b==0 && field.toString().equals(panelBlok5.getTextFieldB5R2id().toString())) CardKues.getCard().next();
                    else if(field.toString().equals(panelBlok5.getTextFieldB5R2iih().toString())) CardKues.getCard().next();
                }
                else if(field.toString().equals(panelBlok5.getTextFieldB5R2iiic().toString())) CardKues.getCard().next();
                return true;
            }
        }
    }
    
    private class ValidasiB5R1a implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (panelBlok5.getTextFieldB5R1b().getText().equals(field.getText()) || panelBlok5.getTextFieldB5R1c().getText().equals(field.getText())) {

                if (Integer.parseInt(field.getText()) == 0) {
                    if (panelBlok5.getTextFieldB5R1b().getText().equals(panelBlok5.getTextFieldB5R1c().getText())) {
                        errorMessage("Tidak boleh 0 semua!");
                        field.setText("");
                        return false;
                    } else {
                        setDisable();
                        B5R1a = Integer.parseInt(field.getText());
                        try {
                            if (Integer.parseInt(panelBlok5.getTextFieldB5R1b().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1c())) {
                                errorMessage("Cek Isian B5R1b kembali!");
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                        try {
                            if (Integer.parseInt(panelBlok5.getTextFieldB5R1c().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1b())) {
                                errorMessage("Cek Isian B5R1c kembali!");
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                        return true;
                    }
                } else if (B5R1a == 0) {
                    setEnable();

                    B5R1a = Integer.parseInt(field.getText());
                    if (field.getText().equals((panelBlok5.getTextFieldB5R1b().getText()))) {
                        errorMessage("Cek Isian B5R1b kembali!");
                    }
                    if (field.getText().equals((panelBlok5.getTextFieldB5R1c().getText()))) {
                        errorMessage("Cek Isian B5R1c kembali!");
                    }
                    return true;
                } else {
                    errorMessage("Urutkan sesuai prioritas!");
                    field.setText("");
                    return false;
                }
            } else {

                if (Integer.parseInt(field.getText()) == 1 || Integer.parseInt(field.getText()) == 2 || Integer.parseInt(field.getText()) == 3) {
                    if (B5R1a == 0) {
                    }
                    if (Integer.parseInt(field.getText()) > jumlahIsian(panelBlok5.getTextFieldB5R1b(), panelBlok5.getTextFieldB5R1c())) {
                        errorMessage("Nilai tidak mungkin " + field.getText());
                        field.setText("");
                        return false;
                    } else {
                        setEnable();
                        B5R1a = Integer.parseInt(field.getText());
                        if (field.getText().equals((panelBlok5.getTextFieldB5R1b().getText()))) {
                            errorMessage("Cek Isian B5R1b kembali!");
                        }
                        if (field.getText().equals((panelBlok5.getTextFieldB5R1c().getText()))) {
                            errorMessage("Cek Isian B5R1c kembali!");
                        }
                        return true;
                    }
                } else if (Integer.parseInt(field.getText()) == 0) {
                    setDisable();
                    if (B5R1a != 0) {
                    }
                    B5R1a = Integer.parseInt(field.getText());
                    try {
                        if (Integer.parseInt(panelBlok5.getTextFieldB5R1b().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1c())) {
                            errorMessage("Cek Isian B5R1b kembali!");
                        }
                    } catch (NumberFormatException numberFormatException) {
                    }
                    try {
                        if (Integer.parseInt(panelBlok5.getTextFieldB5R1c().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1b())) {
                            errorMessage("Cek Isian B5R1c kembali!");
                        }
                    } catch (NumberFormatException numberFormatException) {
                    }
                    return true;
                } else {
                    errorMessage("Nilai hanya 1, 2, 3, atau 0");
                    field.setText("");
                    return false;
                }

            }
        }

        private void setEnable() {
            for (int i = 0; i < 4; i++) {
                    panelBlok5.getGroup()[i].setEnabled(true);                
            }
        }

        private void setDisable() {
            panelBlok5.getTextFieldB5R2ia().setText("");
            panelBlok5.getTextFieldB5R2ib().setText("");
            panelBlok5.getTextFieldB5R2ic().setText("");
            panelBlok5.getTextFieldB5R2id().setText("");
            for (int i = 0; i < 4; i++) {
                    panelBlok5.getGroup()[i].setEnabled(false);
                
            }
        }

        private int jumlahIsian(TextField x, TextField y) {
            boolean fieldXnol = false;
            boolean fieldYnol = false;

            if (x.getText().equals("0")) {
                fieldXnol = true;
            }
            if (y.getText().equals("0")) {
                fieldYnol = true;
            }

            if (fieldXnol == true && fieldYnol == true) {

                return 1;
            } else if ((fieldXnol == true && fieldYnol == false) || (fieldXnol == false && fieldYnol == true)) {

                return 2;
            } else {

                return 3;
            }
        }
    }

    private class ValidasiB5R1b implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (panelBlok5.getTextFieldB5R1a().getText().equals(field.getText()) || panelBlok5.getTextFieldB5R1c().getText().equals(field.getText())) {

                if (Integer.parseInt(field.getText()) == 0) {
                    if (panelBlok5.getTextFieldB5R1a().getText().equals(panelBlok5.getTextFieldB5R1c().getText())) {
                        errorMessage("Tidak boleh 0 semua!");
                        field.setText("");
                        return false;
                    } else {
                        setDisable();
                        B5R1b = Integer.parseInt(field.getText());
                        try {
                            if (Integer.parseInt(panelBlok5.getTextFieldB5R1a().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1c())) {
                                errorMessage("Cek Isian B5R1a kembali!");
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                        try {
                            if (Integer.parseInt(panelBlok5.getTextFieldB5R1c().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1a())) {
                                errorMessage("Cek Isian B5R1c kembali!");
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                        return true;
                    }
                } else if (B5R1b == 0) {
                    setEnable();

                    B5R1b = Integer.parseInt(field.getText());
                    if (field.getText().equals((panelBlok5.getTextFieldB5R1a().getText()))) {
                        errorMessage("Cek Isian B5R1a kembali!");
                    }
                    if (field.getText().equals((panelBlok5.getTextFieldB5R1c().getText()))) {
                        errorMessage("Cek Isian B5R1c kembali!");
                    }
                    return true;
                } else {
                    errorMessage("Urutkan sesuai prioritas!");
                    field.setText("");
                    return false;
                }
            } else {

                if (Integer.parseInt(field.getText()) == 1 || Integer.parseInt(field.getText()) == 2 || Integer.parseInt(field.getText()) == 3) {
                    if (B5R1b == 0) {
                    }
                    if (Integer.parseInt(field.getText()) > jumlahIsian(panelBlok5.getTextFieldB5R1a(), panelBlok5.getTextFieldB5R1c())) {
                        errorMessage("Nilai tidak mungkin " + field.getText());
                        field.setText("");
                        return false;
                    } else {
                        setEnable();
                        B5R1b = Integer.parseInt(field.getText());
                        if (field.getText().equals((panelBlok5.getTextFieldB5R1a().getText()))) {
                            errorMessage("Cek Isian B5R1a kembali!");
                        }
                        if (field.getText().equals((panelBlok5.getTextFieldB5R1c().getText()))) {
                            errorMessage("Cek Isian B5R1c kembali!");
                        }
                        return true;
                    }
                } else if (Integer.parseInt(field.getText()) == 0) {
                    setDisable();
                    if (B5R1b != 0) {
                    }
                    B5R1b = Integer.parseInt(field.getText());
                    try {
                        if (Integer.parseInt(panelBlok5.getTextFieldB5R1a().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1c())) {
                            errorMessage("Cek Isian B5R1a kembali!");
                        }
                    } catch (NumberFormatException numberFormatException) {
                    }
                    try {
                        if (Integer.parseInt(panelBlok5.getTextFieldB5R1c().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1a())) {
                            errorMessage("Cek Isian B5R1c kembali!");
                        }
                    } catch (NumberFormatException numberFormatException) {
                    }
                    return true;
                } else {
                    errorMessage("Nilai hanya 1, 2, 3, atau 0");
                    field.setText("");
                    return false;
                }

            }
        }

        private void setEnable() {
            for (int i = 4; i < 12; i++) {
                    panelBlok5.getGroup()[i].setEnabled(true);                
            }
        }

        private void setDisable() {
            panelBlok5.getTextFieldB5R2iia().setText("");
            panelBlok5.getTextFieldB5R2iib().setText("");
            panelBlok5.getTextFieldB5R2iic().setText("");
            panelBlok5.getTextFieldB5R2iid().setText("");
            panelBlok5.getTextFieldB5R2iie().setText("");
            panelBlok5.getTextFieldB5R2iif().setText("");
            panelBlok5.getTextFieldB5R2iig().setText("");
            panelBlok5.getTextFieldB5R2iih().setText("");
            for (int i = 4; i < 12; i++) {
                    panelBlok5.getGroup()[i].setEnabled(false);                
            }
        }

        private int jumlahIsian(TextField x, TextField y) {
            boolean fieldXnol = false;
            boolean fieldYnol = false;

            if (x.getText().equals("0")) {
                fieldXnol = true;
            }
            if (y.getText().equals("0")) {
                fieldYnol = true;
            }

            if (fieldXnol == true && fieldYnol == true) {

                return 1;
            } else if ((fieldXnol == true && fieldYnol == false) || (fieldXnol == false && fieldYnol == true)) {

                return 2;
            } else {

                return 3;
            }
        }
    }

    private class ValidasiB5R1c implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (panelBlok5.getTextFieldB5R1a().getText().equals(field.getText()) || panelBlok5.getTextFieldB5R1b().getText().equals(field.getText())) {

                if (Integer.parseInt(field.getText()) == 0) {
                    if (panelBlok5.getTextFieldB5R1a().getText().equals(panelBlok5.getTextFieldB5R1b().getText())) {
                        errorMessage("Tidak boleh 0 semua!");
                        field.setText("");
                        return false;
                    } else {
                        setDisable();
                        B5R1c = Integer.parseInt(field.getText());
                        try {
                            if (Integer.parseInt(panelBlok5.getTextFieldB5R1a().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1b())) {
                                errorMessage("Cek Isian B5R1a kembali!");
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                        try {
                            if (Integer.parseInt(panelBlok5.getTextFieldB5R1b().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1a())) {
                                errorMessage("Cek Isian B5R1b kembali!");
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                        return true;
                    }
                } else if (B5R1c == 0) {
                    setEnable();

                    B5R1c = Integer.parseInt(field.getText());
                    if (field.getText().equals((panelBlok5.getTextFieldB5R1a().getText()))) {
                        errorMessage("Cek Isian B5R1a kembali!");
                    }
                    if (field.getText().equals((panelBlok5.getTextFieldB5R1b().getText()))) {
                        errorMessage("Cek Isian B5R1b kembali!");
                    }
                    return true;
                } else {
                    errorMessage("Urutkan sesuai prioritas!");
                    field.setText("");
                    return false;
                }
            } else {

                if (Integer.parseInt(field.getText()) == 1 || Integer.parseInt(field.getText()) == 2 || Integer.parseInt(field.getText()) == 3) {
                    if (B5R1c == 0) {
                    }
                    if (Integer.parseInt(field.getText()) > jumlahIsian(panelBlok5.getTextFieldB5R1a(), panelBlok5.getTextFieldB5R1b())) {
                        errorMessage("Nilai tidak mungkin " + field.getText());
                        field.setText("");
                        return false;
                    } else {
                        setEnable();
                        B5R1c = Integer.parseInt(field.getText());
                        if (field.getText().equals((panelBlok5.getTextFieldB5R1a().getText()))) {
                            errorMessage("Cek Isian B5R1a kembali!");
                        }
                        if (field.getText().equals((panelBlok5.getTextFieldB5R1b().getText()))) {
                            errorMessage("Cek Isian B5R1b kembali!");
                        }
                        return true;
                    }
                } else if (Integer.parseInt(field.getText()) == 0) {
                    setDisable();
                    if (B5R1c != 0) {
                    }
                    B5R1c = Integer.parseInt(field.getText());                    
                    try {
                        if (Integer.parseInt(panelBlok5.getTextFieldB5R1a().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1b())) {
                            errorMessage("Cek Isian B5R1a kembali!");
                        }
                    } catch (NumberFormatException numberFormatException) {
                    }
                    try {
                        if (Integer.parseInt(panelBlok5.getTextFieldB5R1b().getText()) > jumlahIsian(field, panelBlok5.getTextFieldB5R1a())) {
                            errorMessage("Cek Isian B5R1b kembali!");
                        }
                    } catch (NumberFormatException numberFormatException) {
                    }
                    return true;
                } else {
                    errorMessage("Nilai hanya 1, 2, 3, atau 0");
                    field.setText("");
                    return false;
                }

            }
        }

        private void setEnable() {
            for (int i = 12; i < 15; i++) {
                    panelBlok5.getGroup()[i].setEnabled(true);                
            }
        }

        private void setDisable() {
            panelBlok5.getTextFieldB5R2iiia().setText("");
            panelBlok5.getTextFieldB5R2iiib().setText("");
            panelBlok5.getTextFieldB5R2iiic().setText("");
            for (int i = 12; i < 15; i++) {
                    panelBlok5.getGroup()[i].setEnabled(false);
            }
        }

        private int jumlahIsian(TextField x, TextField y) {
            boolean fieldXnol = false;
            boolean fieldYnol = false;

            if (x.getText().equals("0")) {
                fieldXnol = true;
            }
            if (y.getText().equals("0")) {
                fieldYnol = true;
            }

            if (fieldXnol == true && fieldYnol == true) {

                return 1;
            } else if ((fieldXnol == true && fieldYnol == false) || (fieldXnol == false && fieldYnol == true)) {

                return 2;
            } else {

                return 3;
            }
        }
    }
}
